package com.quectel.taskserver.filter;

import com.quectel.util.kit.TraceIdKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener;
import org.apache.shardingsphere.elasticjob.infra.listener.ShardingContexts;
import org.slf4j.MDC;

/**
 * 定任务启动前和完成后
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2022-12-21 14:20:00
 */
@Slf4j
public class JobTraceIdListener implements ElasticJobListener {
    @Override
    public void beforeJobExecuted(ShardingContexts shardingContexts) {
        String traceId = TraceIdKit.getTraceId();
        MDC.put(TraceIdKit.TRACE_ID_KEY, traceId);
        log.debug("=======任务：【{}】开始执行=======", shardingContexts.getJobName());
    }

    @Override
    public void afterJobExecuted(ShardingContexts shardingContexts) {
        log.debug("=======任务：【{}】执行结束=======", shardingContexts.getJobName());
        TraceIdKit.remove();
        MDC.remove(TraceIdKit.TRACE_ID_KEY);
    }

    @Override
    public String getType() {
        return "JobListener";
    }
}
